package com.coship.vas.dapg.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.coship.vas.dapg.contans.PrgrelationContants;
import com.coship.vas.dapg.entity.Prgrelation;
import com.coship.vas.dapg.rest.bean.RestPageResult;
import com.coship.vas.dapg.service.RelationService;
/**
 * 点播、直播节目获取关联信息
 * @author 904912
 *
 */
@Path("/")
@Component("prgrelationRestService")
public class PrgrelationRestService {
    @Autowired
    @Qualifier("prgrelationService")
    private RelationService relationService;
    private static Logger logger = LoggerFactory.getLogger(PrgrelationRestService.class);

    @GET
    @Produces("text/html;charset=utf-8")
    @Path("/getPrgRelation")
    public String getRelationInfo(@QueryParam("version") String version, @QueryParam("prgid") String prgId,
            @QueryParam("desclen") String descLen, @QueryParam("imgscale") String imageScale,
            @QueryParam("imgsize") String imageSize, @QueryParam("maxcount") String maxCount) {
        RestPageResult<List<Prgrelation>> result = new RestPageResult<List<Prgrelation>>();
        if (StringUtils.isBlank(prgId)) {
            result.setRet(-1);// 异常
            result.setRetInfo("prgId:" + PrgrelationContants.REST_EXCEPTION_MSG_BANK);
            JSONObject jsonObj = JSONObject.fromObject(result);
            return jsonObj.toString();
        }
        Map<String, String> params = new HashMap<String, String>();
        params.put(PrgrelationContants.REQUEST_PID, prgId);
        params.put(PrgrelationContants.REQUEST_DESC_LEN, descLen);
        params.put(PrgrelationContants.REQUEST_IMAGE_SCALE, imageScale);
        params.put(PrgrelationContants.REQUEST_IMAGE_SIZE, imageSize);
        params.put(PrgrelationContants.REQUEST_MAX_COUNT, maxCount);
        try {
            List<Prgrelation> relationInfos = relationService.getPrgrelation(params);
            result.setRet(0);
            result.setRetInfo("OK");
            result.setDatas(relationInfos);
        } catch (Exception e) {
            result.setRet(-1);// 异常
            result.setRetInfo(PrgrelationContants.REST_EXCEPTION_MSG_SYSTEM);
            logger.error("获取关联信息失败", e);
        }
        JSONObject jsonObj = JSONObject.fromObject(result);
        return jsonObj.toString();
    }

}
